package com.oxy.util;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringUtil {
	private volatile static SpringUtil inst;
	private ApplicationContext appCtx;
	
	private SpringUtil(ServletContext sc) {
		appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	}
	private SpringUtil(String springCfgFile[]) {
		appCtx = new ClassPathXmlApplicationContext(springCfgFile);
	}
	public static void init(ServletContext sc) {
		if(inst == null) {
			synchronized(SpringUtil.class) {
				inst = new SpringUtil(sc);
			}
		}
	}
	public static void init(String springCfgFile[]) {
		if(inst == null) {
			synchronized(SpringUtil.class) {
				inst = new SpringUtil(springCfgFile);
			}
		}
	}
	private static SpringUtil getInstance() {
		return inst;
	}
	public static Object getBean(String name) {
		return getInstance().appCtx.getBean(name);
	}
	public static ApplicationContext getApplicationContext() {
		return getInstance().appCtx;
	}
}
